<template>
    <div>
        <div>
            <el-input
                    size="small"
                    class="addJobInput"
                    placeholder="添加职称"
                    suffix-icon="el-icon-circle-plus"
                    v-model="jl.name"
                    @keydown.enter.native="addJob">
            </el-input>
            <el-select v-model="jl.titleLevel" size="small" filterable placeholder="请选择职称等级">
                <el-option
                        v-for="item in titleLevels"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-button type="warning" icon="el-icon-search" size="small" @click="addJob" style="margin-left: 15px"
                       round>添加
            </el-button>
            <el-button type="danger" icon="el-icon-delete" size="small" :disabled="this.multipleSelection.length==0" round @click="deleteAll">批量删除
            </el-button>
        </div>
        <div class="jobTable">
            <el-table
                    ref="multipleTable"
                    stripe
                    border
                    fit="true"
                    size="medium"
                    :data="jls"
                    tooltip-effect="dark"
                    style="width: 105%"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职称"
                        width="230">
                </el-table-column>
                <el-table-column
                        prop="titleLevel"
                        label="等级"
                        width="220">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        sortable
                        label="创建时间"
                        width="220">
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="状态"
                        width="120"
                        show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.enabled" type="success">启用</el-tag>
                        <el-tag v-else type="danger">禁用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="primary"
                                icon="el-icon-edit"
                                round
                                @click="showEditView(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                icon="el-icon-delete"
                                round
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog title="编辑职称" :visible.sync="dialogVisible" width="20%">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag style="margin-right: 15px;">职称名称</el-tag>
                        </td>
                        <td>
                            <el-input size="small" v-model="updateJl.name" class="updateJobInput"
                                      suffix-icon="el-icon-suitcase"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag style="margin-right: 15px;margin-top:15px">职称等级</el-tag>
                        </td>
                        <td>
                            <el-select style="margin-top: 15px" v-model="updateJl.titleLevel" size="small" filterable placeholder="请选择职称等级">
                                <el-option
                                        v-for="item in titleLevels"
                                        :key="item"
                                        :label="item"
                                        :value="item">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                </table>
                <el-switch style="margin-top: 25px;margin-left: 15px"
                           v-model="updateJl.enabled"
                           active-color="#13ce66"
                           inactive-color="#ff4949"
                           active-text="启用"
                           inactive-text="禁用">
                </el-switch>
            </div>
            <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false" round>取 消</el-button>
        <el-button size="small" type="primary" @click="toUpdateJl" round>确 定</el-button>
      </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "JobLevelMana",
        data() {
            return {
                jl: {
                    name: '',
                    titleLevel: ''
                },
                titleLevels: [
                    '总监',
                    '初级顾问',
                    '中级顾问',
                    '高级顾问',
                    '经理'
                ],
                jls: [],
                dialogVisible: false,
                updateJl: {
                    id: '',
                    name: '',
                    titleLevel: '',
                    enabled: ''
                },
                multipleSelection: []
            }
        },
        mounted() {
            this.initJls();
        },
        methods: {
            deleteAll() {
                this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']个职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item => {
                        ids += 'ids=' + item.id + '&';
                    })
                    this.deleteRequest('/system/basic/joblevel/' + ids).then(resp => {
                        if (resp) {
                            this.initJls();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            toUpdateJl() {
                if (this.updateJl.name) {
                    this.putRequest('/system/basic/joblevel/', this.updateJl).then(resp => {
                        if (resp) {
                            this.initJls();
                            this.dialogVisible = false;
                        }
                    })
                } else {
                    this.$message.error('职称名称不能为空！')
                }
            },
            addJob() {
                if (this.jl.name && this.jl.titleLevel) {
                    this.postRequest('/system/basic/joblevel/', this.jl).then(resp => {
                        if (resp) {
                            this.initJls();
                        }
                    })
                } else {
                    this.$message.error('职称名称不能为空！')
                }
            },
            showEditView(index, data) {
                Object.assign(this.updateJl, data);
                this.updateJl.createDate = '';
                this.dialogVisible = true;
            },
            handleDelete(index, data) {
                this.$confirm('此操作将永久删除[' + data.name + ']职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/joblevel/' + data.id).then(resp => {
                        if (resp) {
                            this.initJls();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            initJls() {
                this.getRequest('/system/basic/joblevel/').then(resp => {
                    if (resp) {
                        this.jls = resp;
                        this.jl.name = '';
                        this.jl.titleLevel = '';
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .addJobInput {
        width: 320px;
        margin-right: 15px;
    }

    .jobTable {
        margin-top: 15px;
    }

</style>